package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 菜单控制器
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {

    @Reference
    private MenuService menuService;

    @PreAuthorize("hasAnyAuthority('MENU_ADD')")//权限校验
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        log.debug("menu:{}",menu);
        try {
            menuService.add(menu);
            return new Result(true,MessageConst.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ADD_MENU_FAIL);
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")//权限校验
    @RequestMapping("/findPage")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try {
                pageResult = menuService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
                return pageResult;
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(0L,null);
        }
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }

    }

    @PreAuthorize("hasAnyAuthority('MENU_EDIT')")//权限校验
    @RequestMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,MessageConst.ACTION_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }

    @PreAuthorize("hasAnyAuthority('MENU_DELETE')")
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(Integer id){
        try {
            menuService.deleteById(id);
            return new Result(true,MessageConst.DELETE_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,e.getMessage());

        }
    }
}
